//
//  GetCategoryGoodListVm.swift
//  Pods
//
//  Created by eme on 2017/4/10.
//
//

import UIKit
import NetWorkCore
import RxSwift
import SwiftyJSON
import Result
import UtilCore
import AddCarCore
import RickonCore

class GetCategoryGoodListVm: Base_Vm {
    
    let backDate = Variable<[CategoryGoodModel]>([])
    
    let page = Variable(1)
    
    let hasNextPage = Variable(true)
    
    var ppturl = ""
    
    var pptype = ""
    
    let ppturlStatus = Variable("")
    
    let getSKU = PublishSubject<String>()
    
    let backSkuDate = PublishSubject<ProductModel>()
    
    var loading:Observable<Bool>
    
    
    init (
        input:(
        refreshTriger:Observable<Void>,
        loadMoreTriger:Observable<Void>
        
        )
        ){
        
        let requestIndicator = ActivityIndicator()
        self.loading = requestIndicator.asObservable()
        
        super.init()

        let refreshRequest = Observable.of(
            input.refreshTriger,
            reloadTrigger.withLatestFrom(input.refreshTriger)).merge().map{ _ in
                
                return CategoryApi.getCategoryGoodList(pptype: self.pptype, ppturl: self.ppturl, page: 0, length:  AppConfig.pageSize)
                
            }.shareReplay(1)
        
        
        let refreshResponse = refreshRequest.emeRequestApiForArray(CategoryGoodModel.self,activityIndicator: requestIndicator).shareReplay(1)
        
        
        refreshResponse.subscribe(onNext:{[unowned self] result in
            switch result{
            case .success(let data):
                
                self.hasNextPage.value = data.count == AppConfig.pageSize
                self.backDate.value = data
                
            case .failure(let error):
                
                self.refresherror.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        
        let loadMoreRequest = (
            input.loadMoreTriger
            ).map{ _ in
                
                return CategoryApi.getCategoryGoodList(pptype:self.pptype , ppturl: self.ppturl, page: self.page.value - 1, length: AppConfig.pageSize)
                
            }.shareReplay(1)
        
        let loadMoreResponse = loadMoreRequest.emeRequestApiForArray(CategoryGoodModel.self).shareReplay(1)
        
        loadMoreResponse.subscribe(onNext:{[unowned self] result in
            switch result {
            case .success(let data):
                
                self.hasNextPage.value = data.count == AppConfig.pageSize
                self.backDate.value += data
                self.page.value += 1
                
            case .failure(let error):
                
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        refreshRequest.subscribe(onNext:{ _ in
            
            self.hasNextPage.value = true
            self.page.value = 2
            
            }).addDisposableTo(disposeBag)
        
        
        //========--------
        //点击按钮获取
        let request = Observable.of(
            ppturlStatus.asObservable(),
            reloadTrigger.withLatestFrom(ppturlStatus.asObservable())
            ).merge().map{ type in
                
                return CategoryApi.getCategoryGoodList(pptype: self.pptype, ppturl: type, page: 0, length: AppConfig.pageSize)
                
            }.shareReplay(1)
        
        let response = request.emeRequestApiForArray(CategoryGoodModel.self,activityIndicator: requestIndicator).shareReplay(1)
        
        response.subscribe(onNext:{[unowned self] result in
            switch result{
            case.success(let data):
                
                self.hasNextPage.value = data.count == AppConfig.pageSize
                self.backDate.value = data
                
            case .failure(let error):
                
                self.refresherror.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        request.subscribe(onNext:{ _ in
            
            self.hasNextPage.value = true
            self.page.value = 2
            
            }).addDisposableTo(disposeBag)
        
        
        //----------
        //获取SKU
        
//        let skuRequest = getSKU.map{ pdid in
////            return HomePageApi.glproductskus(pdid: pdid)
//            }.shareReplay(1)
//        
//        let skuResponse = skuRequest.emeRequestApiForObj(ProductModel.self).shareReplay(1)
//        
//        skuResponse.subscribe(onNext:{[unowned self] result in
//            switch result{
//            case .success(let data):
//                self.backSkuDate.onNext(data)
//            case .failure(let error):
//                self.error.onNext(error)
//            }
//        }).addDisposableTo(disposeBag)
    }
    
}
